import { request } from '../utils/request';

export interface TimeRangeStats {
  codeLines: number;
  apiChangeCount: number;
  newApiCount: number;
  restRatio: string;
  dubboRatio: string;
  zombieApiCount: number;
  zombieApiRatio: string;
  highFreqApiCount: number;
  mediumFreqApiCount: number;
  lowFreqApiCount: number;
  methodChangeCount: number;
  zombieMethodCount: number;
  zombieMethodRatio: string;
  zombieMethodCodeLines: number;
}

export interface ChangeAnalysisData {
  all: TimeRangeStats;
  halfYear: TimeRangeStats;
  oneYear: TimeRangeStats;
}

/**
 * 获取单元变更分析统计数据
 */
export const getChangeAnalysisStats = async (unitId: string): Promise<ChangeAnalysisData> => {
  const response: any = await request({ url: `/unit/${unitId}/change-analysis`, method: 'get' });

  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '获取变更分析统计数据失败');
};

/**
 * 手动触发变更分析计算
 */
export const triggerChangeAnalysisCalculation = async (unitId: string): Promise<void> => {
  const response: any = await request({ url: `/unit/${unitId}/change-analysis/calculate`, method: 'post' });

  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '触发变更分析计算失败');
};

/**
 * 计算所有单元的变更分析数据
 */
export const calculateAllUnitsChangeAnalysis = async (): Promise<void> => {
  const response: any = await request({ url: '/unit/change-analysis/calculate-all', method: 'post' });

  if (response.code === 0) {
    return response.data;
  }
  throw new Error(response.msg || response.message || '计算所有单元变更分析数据失败');
};